package com.lanou.wuyou.gift.home;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.EditText;

import com.lanou.wuyou.gift.R;
import com.lanou.wuyou.gift.base.BaseFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 17/3/14.
 */

public class HomeFragment extends BaseFragment {

    private String[] titles = {"精选","送男友", "送闺蜜", "送长辈"};
    private TabLayout tabLayout;
    private ViewPager homeVp;
    private HomeVPAdapter adapter;
    private EditText editText;

    public static HomeFragment newInstance(){

        Bundle args = new Bundle();
        HomeFragment fragment = new HomeFragment();
        fragment.setArguments(args);
        return fragment;
    }
    private String[] urls = {
            "http://api.liwushuo.com/v2/channels/100/items_v2?ad=2&gender=1&generation=1&limit=20&offset=0",
            "http://api.liwushuo.com/v2/channels/10/items_v2?gender=1&limit=20&offset=0&generation=1",
            "http://api.liwushuo.com/v2/channels/26/items_v2?gender=1&limit=20&offset=0&generation=1",
            "http://api.liwushuo.com/v2/channels/6/items_v2?gender=1&limit=20&offset=0&generation=1",
            };

    @Override
    public int setLayout() {
        return R.layout.fragment_home;
    }

    @Override
    public void initView(View view) {

        homeVp = (ViewPager) view.findViewById(R.id.view_pager_home);
        adapter = new HomeVPAdapter(getChildFragmentManager());
        homeVp.setAdapter(adapter);

        tabLayout = (TabLayout) view.findViewById(R.id.tab_layout_home);
        tabLayout.setupWithViewPager(homeVp);
        tabLayout.setTabTextColors(Color.parseColor("#949494"),Color.parseColor("#e63b4b"));
        tabLayout.setBackgroundColor(Color.WHITE);
        tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        editText = (EditText) view.findViewById(R.id.home_et);
        editText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), HomeFirstSearchHistory.class);
                startActivity(intent);
            }
        });

        List<Fragment> fragments = new ArrayList<>();
        fragments.add(HomeFirstFragment.newInstance(urls[0]));
        for (int i = 1; i < titles.length; i++) {
            fragments.add(HomeListFragment.newInstance(urls[i]));
        }
        adapter.setFragments(fragments);

        for (int i = 0; i < titles.length; i++) {
            tabLayout.getTabAt(i).setText(titles[i]);
        }
    }

    @Override
    public void initData() {

    }
}
